﻿using biaoqian;
using FastReport.DevComponents.DotNetBar.Controls;
using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Wkol.Controllers
{
    public class HomeController : Controller
    {
        Class1 one = new Class1();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About(string combs = "", int stace = 0, string shulian = "",string shinf="")
        {
            //显示和查找
            if (stace == 1)
            {
                DateTime now = DateTime.Now;
                string CrrentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
                K3CloudApi client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "SAL_DELIVERYNOTICE";
                //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FBillNo,FCustomerID.FName,FMaterialID.FNumber,FMaterialName,FMateriaModel,FQty,FPriceUnitId.FName";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式  and FCancelStatus='A'  and FPlanDeliveryDate >'" + currentDateTime + "'
                var ZBLx = combs;
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FBillNo like'%" + ZBLx + "%' and FSaleOrgId.FName='苏州安派思包装有限公司' and FMaterialName like'%"+ shinf + "%'",
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 120,
                    OrderString = "FBillNo",
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());///查询所有

                /* JsonSerializerSettings settings = new JsonSerializerSettings();
                 string jsongStr = JsonConvert.SerializeObject(result, settings);*/
                //转换成标准文档并遍历
                DataTable dataTable = one.DAtaijs();
                shulian = shulian == "" ? "0" : shulian;
                var ssjinf = 0;
                foreach (var item in result)
                {
                    ssjinf++;
                    var owulia = item[3].ToString();
                    DataRow[] dataRow = dataTable.Select("Material='"+ owulia + "'");
                    if (dataRow.Count() > 0)
                    {
                        var shnd = int.Parse(item[5].ToString());
                        foreach (var item2 in dataRow)
                        {
                            var shu = int.Parse(item2[5].ToString());
                            shu = shu + shnd;
                            item[5] = shu;
                        }
                    }
                    else
                    {

                        if (ssjinf == int.Parse(shulian))
                        {
                            dataTable.Rows.Add(item[0], item[1], item[2], item[3], item[4], item[5], item[6]);
                        }
                        else if (shulian == null || shulian == "0")
                        {
                            dataTable.Rows.Add(item[0], item[1], item[2], item[3], item[4], item[5], item[6]);
                        }
                    }
                }
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(dataTable, settings);

                return Content(jsongStr, "application/json");
            }

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult Yanjin()
        {
            return View();
        }
    }
}